<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>角色管理</title>
<link rel="stylesheet" type="text/css" href="/themes/default/easyui.css" id="swicth-style">
<link rel="stylesheet" type="text/css" href="/css/icon.css">
<link rel="stylesheet" type="text/css" href="/css/common.css">
<link rel="stylesheet" type="text/css" href="/themes/icon.css">
<link rel="stylesheet" type="text/css" href="/themes/color.css">
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="/js/jquery.cookie.js"></script>
<script type="text/javascript" src="/js/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="/js/common.js"></script>
</head>

<body class="easyui-layout" style="padding:5px;">
<fieldset>
    <legend>信息查询</legend>
    <form id="ffSearch" method="post">
        <div id="searchBar">
            <table cellspacing="0" cellpadding="0">
                <tr>
                    <th>角色名称：</th>
                    <td style="width:120px">
                        <input name="name" class="easyui-textbox" type="text" style="width:100px">
                    </td>
                    <th>角色类型：</th>
                    <td><select id="typeSelect" name="type" class="easyui-combobox" data-options="onSelect: function(rec){onSearch()}" style="width:100px">
                        <option value="">全部</option>
                        <option value="666">超级管理员</option>
                        <option value="0">平台公司</option>
                        <option value="1">代理商</option>
                        <option value="2">合作伙伴</option>
                    </select>
                    </td>
                    <th>角色状态：</th>
                    <td style="width:120px">
                        <select id="statusSelect" name="status" class="easyui-combobox" data-options="onSelect: function(rec){onSearch()}" style="width:100px">
                            <option value="0">全部</option>
                            <option value="1">启用</option>
                            <option value="2">禁用</option>
                        </select>
                    </td>
                    <td colspan="2">
                        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="onSearch()">查询</a>&nbsp;&nbsp;
                        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-clear'" onclick="onClear()">重置</a>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</fieldset>

<table id="dg"> </table>

<div id="addOrEdit" class="easyui-window" title="添加角色" data-options="closed:true,iconCls:'icon-save',footer:'#ft'" style="width:450px;height:300px;padding:10px;">
    <form id="ff" method="post">
        <table class="table-blue">
            <tr>
                <th>角色名称：</th>
                <td><input type="hidden" name="id"/><input class="easyui-validatebox textbox" type="text" name="name" data-options="required:true, validType:'maxLength[50]'"/></td>
            </tr>
            <tr>
            <th>角色类型：</th>
            <td><select id="tSelect" name="type" class="easyui-combobox"  style="width:100px" >
                <option value="666">超级管理员</option>
                <option value="0">平台公司</option>
                <option value="1">代理商</option>
                <option value="2">合作伙伴</option>
            </select>
            </td>
            </tr>
            <tr>
                <th>是否是管理员：</th>
                <td><input  type="checkbox" id="ismgr"  onclick="checkChanged();"><input id="isManager" name="isManager" type="hidden" value="1"></td>
            </tr>
        </table>
    </form>
    <div id="ft" style="padding:5px;text-align: right;">
        <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)" onclick="submitForm()" style="width:80px">Ok</a> &nbsp;&nbsp;
        <a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:void(0)" onclick="closeWindow()" style="width:80px">Cancel</a>
    </div>
</div>

<div id="bind" class="easyui-window" title="菜单分配" data-options="closed:true,iconCls:'icon-save',footer:'#ft1'" style="width:300px;height:500px;padding:10px;">
    <ul id="tt"></ul>
    <div id="ft1" style="padding:5px;text-align: right;">
        <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)" onclick="bind()" style="width:80px">Ok</a> &nbsp;&nbsp;
        <a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:void(0)" onclick="closeWindow()" style="width:80px">Cancel</a>
    </div>
</div>

<script type="text/javascript">
    $(function(){
        var dg = $('#dg').datagrid();
        dg.datagrid({
            url:'/v1/api0/all/role/list',
            columns:[[
                {field:'id',checkbox:true},
               <!-- {field:'id',title:'角色ID',width:0},-->
                {field:'name',title:'角色名称',width:100},
                {field:'type',title:'角色类型',width:100,formatter: function(value,row,index){
                    if(value=='0'||value==0||value=="0"){
                     return "平台公司";
                    }else if(value==1){
                    return "代理商";
                    }else if(value=="2"){
                    return "合作伙伴";
                    }else if(value=='666'){
                    return "超级管理员";
                    }
                }},
                {field:'createTime',title:'创建时间',width:100,formatter: formatDateTime},
                {field:'status',title:'状态',width:100,formatter: function(value,row,index){
                    switch(value) {
                        case 1: return "启用";
                        case 2: return "禁用";
                    }
                }},
                {field:'action',title:'操作',width:100,formatter: function(value,row,index){
                    if(row.status == 1){
                        return '<a href="javascript:void(0)" class="easyui-linkbutton" onclick="showBindWindow(\''+row.id+'\')">菜单分配</a> | ' +
                                '<a href="javascript:void(0)" class="easyui-linkbutton" onclick="disable(\''+row.id+'\')">禁用</a>';
                    }else{
                        return '<a href="javascript:void(0)" class="easyui-linkbutton" onclick="showBindWindow(\''+row.id+'\')">菜单分配</a> | ' +
                                '<a href="javascript:void(0)" class="easyui-linkbutton" onclick="enable(\''+row.id+'\')">启用</a>';
                    }
                }}
            ]],
            title:'角色管理',
            pagination: true,
            iconCls: 'icon-application_view_detail',
            singleSelect: false,
            toolbar: [{
                id: 'btnAdd',
                text: '添加',
                iconCls: 'icon-add',
                handler: function () {
                    add();
                }
            }, '-', {
                id: 'btnEdit',
                text: '修改',
                iconCls: 'icon-edit',
                handler: function () {
                    edit();
                }
            }, '-', {
                id: 'btnDelete',
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    del();
                }
            }, '-', {
                id: 'btnReload',
                text: '刷新',
                iconCls: 'icon-reload',
                handler: function () {
                    $("#dg").datagrid("reload");
                }
            }/*, '-', {
                id: 'btnBind',
                text: '菜单分配',
                iconCls: 'icon-plugin',
                handler: function () {
                    $("#dg").datagrid("reload");
                }
            }*/]
        });

        var heightMargin = $("#searchBar").height() + 45;
        $('#dg').resizeDataGrid(heightMargin, 0, 0, 0);
        $(window).resize(function() {
            $('#dg').resizeDataGrid(heightMargin, 0, 0, 0);
        });
    });

    function onSearch(){
        var queryParams = $('#ffSearch').serializeObject();
        $('#dg').datagrid({
            url:'/v1/api0/all/role/list',
            queryParams: queryParams
        });
    }

    function onClear() {
        $('#ffSearch').form('clear');
        $('#statusSelect').combobox('setValue', '0');
        $('#typeSelect').combobox('setValue', '');
        onSearch();
    }

    function add() {
        $('#ff').form('clear');
        $('#tSelect').combobox('setValue', 0);
        $("#ff input[name='id']").val("");
        $('#isManager').val(1);
        $('#addOrEdit').window({title:'添加角色'});
        $('#addOrEdit').window('open');
    }

    function edit() {
        $('#ff').form('clear');
        var row = $('#dg').datagrid('getSelected');
        if (!row){
            top.window.showMessage("请选择角色");
            return;
        }
        $('#ff').form('load',row);
        $("#ff input[name='id']").val(row.id);
        $('body').data('ff-serialize', $('#ff').serialize());
        if($('#isManager').val()=='0'){
        $("#ismgr").prop("checked",true);
        }
        $('#addOrEdit').window({title:'编辑角色'});
        $('#addOrEdit').window('open');
    }

    function submitForm(){
        var id = $("#ff input[name='id']").val();
        var url = id.length ? "/v1/api0/role/edit" : "/v1/api0/role/add";
        var data = $('#ff').serialize();
        if(data == $('body').data('ff-serialize')) {
            top.window.showMessage("数据没有改动");
            return;
        }
        $.ajax({
            type: "post",
            url: url,
            data: data,
            datatype: "json",
            beforeSend: function () {
                if (!$('#ff').form('validate')){
                    return false;
                }
                $('#dg').datagrid('loading');
            },
            success: function (data) {
                if(data.code == 200) {
                    $('#dg').datagrid('reload');
                    top.window.showMessage();
                    closeWindow();
                } else {
                    top.window.showMessage(data.msg);
                    $('#dg').datagrid('loaded');
                }
            },
            error: function () {
                $('#dg').datagrid('loaded');
            }
        });
    }

    function closeWindow(){
        $('#addOrEdit').window('close');
        $('#bind').window('close');
    }

    function del() {
        var ids = [];
        var rows = $('#dg').datagrid('getSelections');
        if(!rows.length) {
            top.window.showMessage("请选择角色");
            return;
        }
        for (var i = 0; i < rows.length; i++) {
            ids.push(rows[i].id);
        }

        top.window.$.messager.confirm('确认', '您确定要删除选择的角色吗？', function(r){
            if (r){
                var url = "/v1/api0/role/delete";
                var data = {ids: ids.join(',')};
                $.ajax(ajaxOption(url, data));
            }
        });
    }

    function disable(id) {
    top.window.$.messager.confirm('确认', '您确定要禁用该角色吗？', function(r){
            if (r){
               var url = "/v1/api0/role/disable";
        var data = {id: id};
        $.ajax(ajaxOption(url, data));
            }
        });

    }

    function enable(id) {
    top.window.$.messager.confirm('确认', '您确定要启用该角色吗？', function(r){
            if (r){
                var url = "/v1/api0/role/enable";
        var data = {id: id};
        $.ajax(ajaxOption(url, data));
            }
        });

    }

    function showBindWindow(id) {
        $('body').data('roleId', id);
        $('#tt').tree({
            url:'/v1/api0/role/resource',
            queryParams: {roleId:id},
            checkbox: true
        });
        $('#bind').window('open');
    }

    function bind() {
        var roleId = $('body').data('roleId');
        var resourceIds = [];
        var nodes = $('#tt').tree('getChecked', ['checked','indeterminate']);
        if(!nodes.length) {
            top.window.showMessage("请选择要绑定的角色");
            return;
        }
        for(var i=0; i<nodes.length; i++){
            resourceIds.push(nodes[i].id);
        }
        var url = "/v1/api0/role/bindResource";
        var data = {roleId:roleId, resourceIds: resourceIds.join(',')};
        $.ajax(ajaxOption(url, data));
    }

    function ajaxOption(url, data) {
        return {
            type: "post",
            url: url,
            data: data,
            datatype: "json",
            beforeSend: function () {
                $('#dg').datagrid('loading');
            },
            success: function (data) {
                if(data.code == 200) {
                    $('#dg').datagrid('reload');
                    top.window.showMessage();
                    closeWindow();
                } else {
                    top.window.showMessage(data.msg);
                    $('#dg').datagrid('loaded');
                }
            },
            error: function () {
                $('#dg').datagrid('loaded');
            }
        }
    }

    function checkChanged(){
       var ischecked = $("#ismgr").is(':checked');
       if(ischecked){
        $('#isManager').val(0);
       }else{
       $('#isManager').val(1);}
    }
</script>
</body>
</html>
